package com.code.commons.util;

import lombok.Synchronized;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TimeUtil {

    /**
     * 注意保证线程的安全性，看一下ali的规范
     */
    SimpleDateFormat ddformat = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat ddSimpleFormat = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat monthformat = new SimpleDateFormat("yyyy-MM");
    SimpleDateFormat HHformat = new SimpleDateFormat("yyyy-MM-dd HH");
    SimpleDateFormat mmformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    SimpleDateFormat ssformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat SSSformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    SimpleDateFormat Hmsformat = new SimpleDateFormat("HH:mm:ss");

    /**
     * 获取失效时间
     *
     * @return 失效时间
     */
    @Synchronized
    public String getInvalidTime() {
        long currentTime = System.currentTimeMillis();
        // 半小时后
        currentTime += 30 * 60 * 1000;

        return ssformat.format(new Date(currentTime));
    }

    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    @Synchronized
    public String now() {
        return ssformat.format(new Date());
    }

    /**
     * 获取当前时间(精确到毫秒)
     *
     * @return 当前时间
     */
    @Synchronized
    public String nowMs() {
        return SSSformat.format(new Date());
    }

    /**
     * 今天
     *
     * @return 今天
     */
    @Synchronized
    public String today() {
        return ddformat.format(new Date());
    }

    /**
     * 今天的年月
     *
     * @return 今天的年月
     */
    @Synchronized
    public String todayMonth() {
        return monthformat.format(new Date());
    }

    /**
     * 当前时分秒
     */
    @Synchronized
    public String currentHms() {
        return Hmsformat.format(new Date());
    }

    /**
     * 获取时分秒
     */
    @Synchronized
    public String getHms(String times) throws Exception {
        return Hmsformat.format(ssformat.parse(times));
    }

    /**
     * 当前年月日时分
     */
    @Synchronized
    public String currentHm(String date) throws Exception {
        return mmformat.format(mmformat.parse(date));
    }

    /**
     * 获取简单的日期
     */
    @Synchronized
    public String getSimpleDate(String date) throws Exception {
        return ddSimpleFormat.format(ddSimpleFormat.parse(date));
    }

    /**
     * 获取年月日，时分秒
     */
    @Synchronized
    public Date getSimpleDateForHms(Date date) throws Exception {
        return ssformat.parse(ssformat.format(date));
    }


    /**
     * 和当前小时比较
     *
     * @param time 参数时间
     * @return 1:大于当前时间 0:等于当前时间 -1:小于当前时间 -2:有错误
     */
    @Synchronized
    public int compareToCurrentHour(String time) {
        Date now = new Date();
        int result;
        try {
            Date date = ssformat.parse(time);
            now = HHformat.parse(HHformat.format(now));

            if (date.getTime() > now.getTime()) {
                result = 1;
            } else if (date.getTime() < now.getTime()) {
                result = -1;
            } else {
                result = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = -2;
        }
        return result;
    }

    /**
     * 和当前分钟比较
     *
     * @param time 参数时间
     * @return 1:大于当前时间 0:等于当前时间 -1:小于当前时间 -2:有错误
     */
    @Synchronized
    public int compareToCurrentMinute(String time) {
        Date now = new Date();
        int result;
        try {
            Date date = ssformat.parse(time);
            now = mmformat.parse(mmformat.format(now));

            if (date.getTime() > now.getTime()) {
                result = 1;
            } else if (date.getTime() < now.getTime()) {
                result = -1;
            } else {
                result = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = -2;
        }
        return result;
    }


    /**
     * 开始结束时间计算时间差
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return 时间差
     */
    @Synchronized
    public String calHms(String start, String end) {
        String cost;
        try {
            long endTimeStamp = Hmsformat.parse(end).getTime();
            long startTimeStamp = Hmsformat.parse(start).getTime();

            // 规避8小时问题
            cost = Hmsformat.format(new Date(endTimeStamp - startTimeStamp - 8
                    * 60 * 60 * 1000));
        } catch (Exception e) {
            e.printStackTrace();
            cost = "00:00:00";
        }
        return cost;
    }

    /**
     * 汇总耗时列表
     *
     * @param hmsList 耗时列表
     * @return 耗时汇总
     */
    @Synchronized
    public String calTotalHms(List<String> hmsList) {
        long timeStamp = 0;
        String totalHms;
        for (String hms : hmsList) {
            try {
                timeStamp += Hmsformat.parse(hms).getTime() + 8 * 60 * 60
                        * 1000;
            } catch (Exception e) {
                e.printStackTrace();
                timeStamp += 0;
            }
        }
        try {
            totalHms = Hmsformat
                    .format(new Date(timeStamp - 8 * 60 * 60 * 1000));
        } catch (Exception e) {
            e.printStackTrace();
            totalHms = "00:00:00";
        }

        return totalHms;
    }

    /**
     * 比较时间大小
     *
     * @param DATE1
     * @param DATE2
     * @return
     */
    @Synchronized
    public int compare_date(String DATE1, String DATE2) {
        try {
            Date dt1 = ddformat.parse(DATE1);
            Date dt2 = ddformat.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
//				System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
//				System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 把时间往后推n年
     */
    @Synchronized
    public String pushNYearsForDate(String date, Integer n) {
        Calendar calendar = Calendar.getInstance();
        Date nowDate = null;
        try {
            nowDate = ssformat.parse(date);
            calendar.setTime(nowDate);
            calendar.add(Calendar.YEAR, n);
            nowDate = calendar.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ssformat.format(nowDate);
    }

    /**
     * 把时间往后推n天
     */
    @Synchronized
    public String pushNDaysForDate(String date, Integer n, SimpleDateFormat dateFormat) {
        Calendar calendar = Calendar.getInstance();
        Date nowDate = null;
        try {
            calendar.setTime(dateFormat.parse(date)); //需要将date数据转移到Calender对象中操作
            calendar.add(calendar.DATE, n);//把日期往后增加n天.正数往后推,负数往前移动
            nowDate = calendar.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateFormat.format(nowDate);
    }

    /**
     * 计算天数差
     *
     * @param DATE1 以前的时间
     * @param DATE2 当前时间
     * @return
     */
    @Synchronized
    public int timeDaysDifference(String DATE1, String DATE2) {
        int days = 0;
        try {
            String fromDate = mmformat.format(DATE1);
            String toDate = mmformat.format(DATE2);
            long from = mmformat.parse(fromDate).getTime();
            long to = mmformat.parse(toDate).getTime();
            days = (int) ((to - from) / (1000 * 60 * 60 * 24));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return days;
    }

    /**
     * 计算小时差
     *
     * @param DATE1 以前的时间
     * @param DATE2 当前时间
     * @return
     */
    @Synchronized
    public int timeHoursDifference(String DATE1, String DATE2) {
        int hours = 0;
        try {
            String fromDate = mmformat.format(mmformat.parse(DATE1));
            String toDate = mmformat.format(mmformat.parse(DATE2));
            long from = mmformat.parse(fromDate).getTime();
            long to = mmformat.parse(toDate).getTime();
            hours = (int) ((to - from) / (1000 * 60 * 60));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return hours;
    }

    /**
     * 计算分钟差
     *
     * @param DATE1 以前的时间
     * @param DATE2 当前时间
     * @return
     */
    @Synchronized
    public int timeMinutesDifference(String DATE1, String DATE2) {
        int minutes = 0;
        try {
            String fromDate = mmformat.format(DATE1);
            String toDate = mmformat.format(DATE2);
            long from = mmformat.parse(fromDate).getTime();
            long to = mmformat.parse(toDate).getTime();
            minutes = (int) ((to - from) / (1000 * 60));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return minutes;
    }

    public static void main(String[] args) {
        /*TimeUtil timeUtil = new TimeUtil();
        String date = "2017-08-01 00:00:00";
		Integer n = 4;
		System.out.println("推"+n+"年的日期为"+	timeUtil.pushNYearsForDate(date, n));*/
        // System.out.print(timeUtil.pause("[{\"end\":\"10:39:42\",\"start\":\"10:39:23\"}]",
        // "1"));
        // System.out.print(timeUtil.calHms("00:12:00", "23:12:22"));
        // List<String> hmsList = new ArrayList<>();
        // hmsList.add("00:12:00");
        // hmsList.add("00:24:12");
        // hmsList.add("11:13:00");
        // System.out.println(timeUtil.calTotalHms(hmsList));

        // System.out.println(timeUtil.calCost("02:00:00", "12:00:00",
        // "[{\"end\":\"10:39:42\",\"start\":\"10:39:23\"}, {\"end\":\"10:39:42\",\"start\":\"10:39:23\"}]"));
        // System.out.println(timeUtil.calCost("02:00:00", "12:00:00",
        // "[{\"end\":\"10:39:42\",\"start\":\"10:39:23\"}]"));
//		System.out.println(timeUtil.calCost("02:00:00", "12:00:00", null));
        // System.out.println(timeUtil.calCost("02:00:00", "12:00:00",
        // "[{\"end\":\"10:39:42\",\"start\":\"10:39:23\"}, {\"end\":\"10:39:42\",\"start\":\"10:39:23\"}, {\"end\":\"10:39:42\",\"start\":\"10:39:23\"}]"));
    }

}
